package com.xyq.job.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xyq.common.core.constants.CacheConstants;
import com.xyq.common.redis.service.RedisService;
import com.xyq.job.domain.question.HotQuestion;
import com.xyq.job.mapper.QuestionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class QuestionXXLJob {

    @Autowired
    private RedisService redisService;

    @Autowired
    private QuestionMapper questionMapper;

    @XxlJob("hotQuestionListHandler")
    public void hotQuestionListHandler() {
        log.info("*******执行刷新热门题目自动任务***************");
        List<HotQuestion> hotQuestionList = questionMapper.selectHotQuestionList();

        if(CollectionUtil.isEmpty(hotQuestionList)) return;

        redisService.deleteObject(getHotQuestionListKey());
        // 刷新缓存
        redisService.rightPushAll(getHotQuestionListKey(), hotQuestionList);
    }

    // 获取热门题目key
    private String getHotQuestionListKey() {
        return CacheConstants.HOT_QUESTION_LIST;
    }
}
